var ex = require('express');
var userdao = require(process.env.ROOT +'/model/userdao.js');//引入用户dao
var qs = require('querystring');
var router = ex.Router();

//用户登录
router.post('/userLogin',function(req,res,next){
		var alldata='';
		req.addListener('data',function(chunk){
			alldata+=chunk;
		});
		req.addListener('end',function(){
			var dataObj = qs.parse(alldata);
			//console.log(dataObj.userName+'==========='+dataObj.userPasswd);
			var query = userdao.User.find({userName:dataObj.userName,userPasswd:dataObj.userPasswd});
			query.exec(function (err, docs) {
				if(err){
						console.log("err="+err);
						res.end("1");
				}else{
					//console.log('docs'+docs);
					//保存session
					req.session.user = dataObj.userName;
					req.session.passwd = dataObj.userPasswd;
					//console.log("user is login = "+req.session.user+"-----"+req.session.passwd);
					res.end("2");
				}
			});	
		});
});


//用户注册 发送验证码
router.post('/sendSMS',function(req,res,next){
	var alldata='';
	req.addListener('data',function(chunk){
		 alldata+=chunk;
	});
	req.addListener('end',function(){
		
		var dataObj = qs.parse(alldata);
		var user = dataObj.user;
		var telnum = dataObj.phone.toString();
		
		TopClient = require(process.env.ROOT +'/libs/AliDaYu_sendSMS/topClient').TopClient;//引入阿里大鱼发送短信的
		var client = new TopClient({
			'appkey': '23427178',
			'appsecret': 'a7a38ef96c956568b6642213a5d6cd1f',
			'REST_URL': 'http://gw.api.taobao.com/router/rest'
		});
		var code = Math.floor(Math.random() * 9000 + 1000).toString();
		//console.log(telnum+'==========='+code);
		req.session.user = user;
		req.session.telnum = telnum;
		req.session.code = code; 
		//console.log(req.session.telnum+'=====allrady======'+req.session.code+'======='+req.session.id);
		client.execute('alibaba.aliqin.fc.sms.num.send', {
			'extend':'biluo',
			'sms_type':'normal',
			'sms_free_sign_name':'sk碧落',
			'sms_param':'{"code":"'+code+'","product":"biluo"}',
			'rec_num':''+telnum+'',
			'sms_template_code':'SMS_12615137'
		}, (error, response) => {
			  if (error) {
				  console.log(error);
				 res.json("1");
			  } else {
				 res.json("2");
			  }

		});
	});

});




//注册用户
router.post('/registerUser',function(req,res,next){
		console.log("-------registerUser------------");
		var alldata='';
		req.addListener('data',function(chunk){
			alldata+=chunk;
		});
		req.addListener('end',function(){
			var dataObj = qs.parse(alldata);
			//console.log(dataObj.username+'==========='+dataObj.mypasswd+"--"+dataObj.myphone+"---"+dataObj.mycode);
			//console.log(req.session.telnum+'======register====='+req.session.code+'======='+req.session.id);
			if(req.session.telnum==dataObj.myphone && req.session.code==dataObj.mycode  ){
						//存储数据
						var user = new userdao.User({
							username: dataObj.username,
							mypasswd: dataObj.mypasswd,
							myphone: dataObj.myphone	
						})
						//保存数据库
						user.save(function(err) {
							if (err) {
								console.log('保存失败');
								res.end('1');
								return;
							}else{
								//保存session
								req.session.user = user.username;
								req.session.passwd = user.mypasswd;
								res.end('2');
							}	
						});	
			}else{
				  res.end('1');
			}
		});
});


module.exports = router;
